using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ECommerce.Library;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using System.Collections.Generic;
using ECommerce.Library.Web;

public partial class controls_wucCoupon : BaseControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void BindData()
    {
        ReadWriteCustomCollection<Coupon> coupons = Coupon.GetActiveCouponList("CouponName ASC");
        if (coupons.Count > 0)
        {
            rptCouponDiscount.DataSource = coupons;
            rptCouponDiscount.DataBind();
            pnlEmptyList.Visible = false;
            pnlCouponList.Visible = true;
        }
        else
        {
            pnlEmptyList.Visible = true;
            pnlCouponList.Visible = false;

        }

    }

    protected void rptCouponDiscount_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    protected void rptCouponDiscount_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Coupon coupon = (Coupon)e.Item.DataItem;
            Label lblCouponName = (Label)e.Item.FindControl("lblCouponName");
            lblCouponName.Text = coupon.CouponName;

            Label lblDescription = (Label)e.Item.FindControl("lblDescription");
            Image imgCouponImage = (Image)e.Item.FindControl("imgCouponImage");
            if (coupon.CouponType == CouponType.Text)
            {
                lblDescription.Text = coupon.CouponDescription;
                imgCouponImage.Visible = false;
            }
            else
            {
                imgCouponImage.ImageUrl = ImageResizer.CheckImageMissing(coupon.CouponImageSmallThumbnail, SiteSettings.NoImageSmallThumbnailUrl);
                lblDescription.Visible = false;
            }

            Label lblDiscount = (Label)e.Item.FindControl("lblDiscount");
            if (coupon.CalculationIn == CouponCalculationIn.Dollar)
            {
                lblDiscount.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(coupon.CouponAmount);
            }
            else
            {
                lblDiscount.Text = ECommerce.Library.ECommerce.Utility.FormatPercentage(coupon.CouponAmount);
            }

            Button btnSendToFriend = (Button)e.Item.FindControl("btnSendToFriend");
            btnSendToFriend.CommandArgument = coupon.CouponId.ToString();
        }
    }

    protected void Send_To_Friend(object sender, CommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "SendToFriend")
            {
                int couponId;
                int.TryParse(e.CommandArgument.ToString(), out couponId);
                Coupon coupon = Coupon.Load(couponId);
                if (coupon != null)
                {
                    pnlPopup.Visible = true;
                    lblCouponName.Text = coupon.CouponName;
                    lblResponse.Visible = false;
                    tbxMessage.Text = string.Empty;
                    tbxEmail1.Text = string.Empty;
                    tbxEmail2.Text = string.Empty;
                    tbxEmail3.Text = string.Empty;
                    tbxEmail4.Text = string.Empty;
                    tbxEmail5.Text = string.Empty;
                    hdfCouponId.Value = couponId.ToString();
                    upnPopup.Update();
                }
            }
        }
        catch
        {

        }
    }
    protected void Cancel_Click(object sender, EventArgs e)
    {
        pnlPopup.Visible = false;
    }
    protected void SendCouponMail_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            int couponId;
            int.TryParse(hdfCouponId.Value, out couponId);
            Coupon coupon = Coupon.Load(couponId);
            if (coupon != null)
            {
                List<string> emailRecipients = new List<string>();

                if (RegExUtil.IsEmail(tbxEmail1.Text.Trim()))
                {
                    emailRecipients.Add(tbxEmail1.Text.Trim());
                }
                if (RegExUtil.IsEmail(tbxEmail2.Text.Trim()))
                {
                    emailRecipients.Add(tbxEmail2.Text.Trim());
                }
                if (RegExUtil.IsEmail(tbxEmail3.Text.Trim()))
                {
                    emailRecipients.Add(tbxEmail3.Text.Trim());
                }
                if (RegExUtil.IsEmail(tbxEmail4.Text.Trim()))
                {
                    emailRecipients.Add(tbxEmail4.Text.Trim());
                }
                if (RegExUtil.IsEmail(tbxEmail5.Text.Trim()))
                {
                    emailRecipients.Add(tbxEmail5.Text.Trim());
                }


                AutoGear.BLL.Address billingAddress = AutoGearSessionManager.BillingAddress;
                if (emailRecipients.Count > 0 && billingAddress != null && Email.SendCouponEmail(billingAddress, coupon, tbxMessage.Text.Trim(), emailRecipients))
                {
                    lblResponse.Text = "<span style='color:green;font-size:11px;font-weight:bold;'>Coupon is sent by email</span>";
                }
                else
                {
                    lblResponse.Text = "<span style='color:red;font-size:11px;font-weight:bold;'>Mail Send Failed</span>";
                }
                lblResponse.Visible = true;
            }
        }
    }
}
